package com.yanqu.road.server.cmd.relatives;

import com.yanqu.road.entity.enums.eLogMoneyType;
import com.yanqu.road.entity.relatives.RelativesInfo;
import com.yanqu.road.logic.config.GameConfig;
import com.yanqu.road.pb.relatives.RelativesProto;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.player.CurrencyModule;
import com.yanqu.road.server.gameplayer.module.relatives.RelativesModule;
import com.yanqu.road.server.manger.relatives.RelativesMgr;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.Protocol;
import com.yanqu.road.utils.property.Property;
import com.yanqu.road.utils.property.PropertyHelper;

import java.math.BigInteger;
import java.util.Map;


@Cmd(code = Protocol.S_RELATIVES_RECEIVE_GIFT, desc = "领取赠礼")
public class ReceiveGiftCmd extends AbstractCommand {

    private long relativesId;
    private boolean oneKey;
    private String reward;
    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        RelativesProto.ReceiveGiftReqMsg reqMsg = RelativesProto.ReceiveGiftReqMsg.parseFrom(packet.getBytes());
        relativesId = reqMsg.getRelativesId();
        oneKey = reqMsg.getOneKey();
        return true;
    }

    @Override
    public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
        int times = player.getModule(RelativesModule.class).getGiftTimes(false);
        int maxTimes = RelativesMgr.getMaxDayReceiveTravelPower(player);
        if (times >= maxTimes) {
            ret = GameErrorCode.E_RELATIVES_RECEIVE_OVER;
            return;
        }
        int count = 0;
        if (oneKey) {
            times = maxTimes - times;
            Map<Long, RelativesInfo> relativesMap = player.getModule(RelativesModule.class).getRelativesMap();
            for (Long relativesId : relativesMap.keySet()) {
                if (times == count) {
                    break;
                }
                RelativesInfo relativesInfo = player.getModule(RelativesModule.class).getRelativesInfo(relativesId);
                if (relativesInfo == null || relativesInfo.getStatus() == 0) {
                    ret = GameErrorCode.E_RELATIVES_TIMES_NOT_ENOUGH;
                    continue;
                }
                ret = player.getModule(RelativesModule.class).receiveGift(relativesId);
                if (ret == 0) {
                    count++;
                }
            }
            if (count == 0) {
                ret = GameErrorCode.E_RELATIVES_CANNOT_RECEIVE_GIFT;
                return;
            }
            ret = 0;
        } else {
            RelativesInfo relativesInfo = player.getModule(RelativesModule.class).getRelativesInfo(relativesId);
            if (relativesInfo == null || relativesInfo.getStatus() == 0) {
                ret = GameErrorCode.E_RELATIVES_TIMES_NOT_ENOUGH;
                return;
            }
            ret = player.getModule(RelativesModule.class).receiveGift(relativesId);
            if (ret == 0) {
                count++;
            }
        }
        // 加道具
        Property property = PropertyHelper.parseStringToProperty(GameConfig.RELATIVES_GIFT_CONFIG);
        Property newProperty = new Property();
        int finalCount = count;
        property.getGoods().forEach((k, v)->{
            newProperty.addProperty(k, v.multiply(BigInteger.valueOf(finalCount)));
        });
        player.getModule(CurrencyModule.class).addCurrency(newProperty, eLogMoneyType.Relatives, eLogMoneyType.RelativesGift);
        // 同步赠礼列表
        player.getModule(RelativesModule.class).syncGiftList();
        reward = PropertyHelper.parsePropertyToString(newProperty);
    }

    @Override
    public void sendData(GamePlayer player) {
        RelativesProto.ReceiveGiftRespMsg.Builder respMsg = RelativesProto.ReceiveGiftRespMsg.newBuilder();
        respMsg.setRet(ret);
        if (ret == 0) {
            respMsg.setReward(reward);
        }
        player.sendPacket(Protocol.U_RELATIVES_RECEIVE_GIFT, respMsg);
    }
}
